<template>
  <div class="actions-container">
    <a-button @click="handlerCancel">返回</a-button>
    <a-button type="primary" @click="handlermodify">
      编辑
    </a-button>
    <a-button @click="handlerActive">{{ user.status ? '停用' : '恢复' }}</a-button>
    <a-button @click="handlerResetPWD">重置密码</a-button>
    <a-button type="danger" ghost @click="handlerDeleteUser">删除</a-button>
  </div>
</template>

<script>
import props from './props'
import { changeActive, deleteUsers, resetUserPWD } from '@/api/flow/user'

export default {
  mixins: [props],

  computed: {
    uid () {
      return this.$route.params.uid
    },
  },

  methods: {
    // 取消
    handlerCancel () {
      this.$router.go(-1)
    },

    // 重置用户密码
    handlerResetPWD () {
      resetUserPWD(this.uid)
        .then((data) => {
          this.$notification.success({
            message: '重置密码成功！',
            description: data.reset_password.toString(),
          })

          this.$emit('update')
        })
    },

    // 删除用户
    handlerDeleteUser () {
      try {
        this.deleteUsersRequest([this.uid])
      } catch (error) {
        console.log(RoleAccount)
      }
    },

    // 改变用户状态
    async handlerActive () {
      try {
        await changeActive(this.uid)
        this.$emit('update')
      } catch (error) {
      }
    },

    // 删除用户
    async deleteUsersRequest (users) {
      try {
        await deleteUsers(users)

        this.$router.push({
          name: 'RoleAccount',
        })

         this.$notification.success({
          message: '删除成功!',
          duration: 1,
        })
      } catch (error) {
        console.log(error)
      }
    },

    // 修改
    handlermodify () {
      const { uid } = this
      this.$router.push({
        name: 'AccountEdit',
        params: {
          uid,
        },
      })
    },
  },
}

</script>

<style lang="scss" scoped>
.actions-container {
  button {
    margin: 0 $margin-base;
  }
}
</style>
